var app = getApp();
var httpUtil = require("../utils/httpUtil.js")
var loginUtil = require("../utils/loginUtil.js")
var host = app.data.host
var sendCodeUrl = host + "sendCode"
var getUserUrl = host + "getUserInfo"
var loginUrl = host + "login"
var updateUserInfoUrl = host + "updateCustomerInfo"
var getBalanceUrl = host + "getBalance"
//发送验证码
function sendCode(cellphone, cb) {
  var data = {
    cellphone: cellphone
  }
  httpUtil.get(sendCodeUrl, data, res=>{
    if(res.status==0){
      wx.setStorage({
        key: 'sessionId',
        data: res.data.sessionId,
      })
    }
    cb(res)
  })
}
//获取用户信息
function getUserInfo(openid, cb) {
  var data = {
    openid: openid
  }
  httpUtil.get(getUserUrl, data, cb)
}
//登录
function login(data, cb) {
  loginUtil.getOpenId(openid => {
    data.openid = openid
    httpUtil.get(loginUrl, data, cb)
  })

}
//更新用户信息
function updateUserInfo(data, cb) {
  data.id = app.data.user.id
  httpUtil.post(updateUserInfoUrl, data, cb)
}
//获取用户余额
function getBalance(cb) {
  var data = {
    cusId: app.data.user.id
  }
  httpUtil.get(getBalanceUrl,data,cb)
}
module.exports = {
  sendCode: sendCode,
  getUserInfo: getUserInfo,
  login: login,
  updateUserInfo: updateUserInfo,
  getBalance: getBalance,
}